package com.splashcard.tool.util;

import com.splashcard.cardsFace.SplashCard;
import com.splashcard.message.ExceptionMessage;
import com.splashcard.message.ParaMessage;
import com.splashcard.message.ResultMessage;
import com.splashcard.message.SplashCardMessage;

public class EngineKernel {
	public interface KernelFinishCallBack {
		public void OnFinish(String groupName, int groupIndex,
				SplashCardMessage result);
	}

	public static Thread exec(final String groupName, final int groupIndex,
			final SplashCard card, final ParaMessage para,
			final KernelFinishCallBack callBack) {

		Thread tmp = new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					long start = System.currentTimeMillis();
					card.OnCardStart(this, para);
					card.OnCardExecue(this, para);
					card.OnCardFinish(this, para);
					ResultMessage map = card.getResult();
					if (map == null) {
						map = new ResultMessage();
						map.put("tips", "result is null");
					}
					long end = System.currentTimeMillis();
					map.put("threadRunTime", end - start);
					map.put("name", card.getName());
					map.put("detaul", card.getCardDetail());
					if (callBack != null) {
						callBack.OnFinish(groupName, groupIndex, map);
					}

				} catch (Exception e) {
					e.printStackTrace();
					ExceptionMessage map = new ExceptionMessage();
					map.put("isException", "true");
					map.put("Exception", e);
					if (callBack != null) {
						callBack.OnFinish(groupName, groupIndex, map);
					}
				}
			}
		};
		tmp.start();
		return tmp;
	}
}
